<?php
// $Id$

/**
 * Controller_PartnerOfficer 控制器
 */
class Controller_PartnerOfficer extends Controller_Abstract
{

	function actionIndex()
	{
        // 为 $this->_view 指定的值将会传递数据到视图中
		# $this->_view['text'] = 'Hello!';
	}
    function actionNewPartnerOfficer()
    {
        $form = new Form_NewPartnerOfficer(url('partnerofficer/newpartnerofficer'));
        if ( $this->_context->isPOST() && $form->validate($_POST) )
        {
            try
            {
                $partnerofficer = new PartnerOfficer($form->values());
                $values = $form->values();
                $partnerofficer->partner_name = $values["partner_name"];
                $partnerofficer->partner_officer_name = $values["partner_officer_name"];
                $partnerofficer_querry = PartnerOfficer::find("partner_officer_name = ? AND partner_name = ?", $partnerofficer->partner_officer_name, $partnerofficer->partner_name)->getOne();
                if ( $partnerofficer_querry != null && $partnerofficer_querry->partner_officer_id != null )
                {
                    $ret = array();
                    $ret["answer"] = "new_partnerofficer";
                    $ret["ret_code"] = -1;
                    $ret["info"] = "";
                    $ret["data"] = "";
                    $callback = $values["callback"];
                    echo $callback .  "(" . json_encode($ret) . ")";
                    exit();
                }
                else
                {
                    $time_now = gettimeofday();
                    
                    $partnerofficer->partner_officer_id  = ($time_now["sec"] + "") . $time_now["usec"];
                    $partnerofficer->personal_id = $values["personal_id"];
                    $partnerofficer->email = $values["email"];
                    $partnerofficer->msn = $values["msn"];
                    $partnerofficer->qq = $values["qq"];
                    $partnerofficer->phone = $values["phone"];
                    $partnerofficer->cellphone = $values["cellphone"];
                    $partnerofficer->gender = $values["gender"];
                    $partnerofficer->age = $values["age"];
                    $partnerofficer->description = $values["description"];
                    
                    $address_live   = new Address();
                    $address_mailing= new Address();
                    
                    $address_live->address_id  = $partnerofficer->partner_officer_id . "l";
                    $address_live->province = $values["province_live"];
                    $address_live->city = $values["city_live"];
                    $address_live->district = $values["district_live"];
                    $address_live->street = $values["street_live"];
                    $address_live->postcode = $values["postcode_live"];
                    $address_live->phone = $values["phone_live"];
                    $address_live->cellphone = $values["phone_live"];
                    $address_live->name = $partnerofficer->partner_officer_name;
                    $address_live->save(99,'create');

                    
                    $address_mailing->address_id  = $partnerofficer->partner_officer_id . "m";
                    $address_mailing->province = $values["province_mailing"];
                    $address_mailing->city = $values["city_mailing"];
                    $address_mailing->district = $values["district_mailing"];
                    $address_mailing->street = $values["street_mailing"];
                    $address_mailing->postcode = $values["postcode_mailing"];
                    $address_mailing->cellphone = $values["phone_live"];
                    $address_mailing->name = $partnerofficer->partner_officer_name;
                    $address_mailing->save(99,'create');

                    $partnerofficer->address_live_id = $address_live->address_id;
                    $partnerofficer->address_mailing_id = $address_mailing->address_id;
                    $partnerofficer->save(99,'create');
                    
                    $ret = array();
                    $ret["answer"] = "new_partnerofficer";
                    $ret["ret_code"] = 1;
                    $ret["info"] = "";
                    $ret["data"] = "";
                    $callback = $values["callback"];
                    echo $callback .  "(" . json_encode($ret) . ")";
                    exit();
                }
            }
            catch (Exception $ex)
            {
                print_r($ex);
                exit();
            }
        }
    }
}


